<template>
  <div class="app-container">

<div style="margin: auto;width:720px;">
    <!-- 添加或修改复核配置对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body> -->
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" >
        <el-form-item label="最低复核通过人数" prop="userCount">
          <el-input-number v-model="form.userCount" controls-position="right" :min="0"  />
        </el-form-item>
        <el-form-item label="最低包含HR人数" prop="containhrCount">
          <el-input-number v-model="form.containhrCount" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">复核人信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSlCheckUser">添加</el-button>
          </el-col>
          <UserMultiSelect ref="userSelect" @onSelected="userSelected"></UserMultiSelect>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSlCheckUser">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="slCheckUserList" :row-class-name="rowSlCheckUserIndex" @selection-change="handleSlCheckUserSelectionChange" ref="slCheckUser">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="用户" prop="checkNick" >
           <template slot-scope="scope">
             {{scope.row.checkNick}}
              <!-- <el-input v-model="scope.row.checkNick" placeholder="请输入昵称" /> -->
            </template>
          </el-table-column>
          <el-table-column label="是否HR" prop="isHr">
            <template slot-scope="scope">
              <el-select v-model="scope.row.isHr" placeholder="请选择是否HR">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <br>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary"  @click="submitForm">保 存</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
      </div>
    <!-- </el-dialog> -->
  </div>
</template>

<script>
import { listCheckconfig, getCheckconfig, delCheckconfig, addCheckconfig, updateCheckconfig,isUniqueUser } from "@/api/sl/checkconfig";
import UserMultiSelect from "@/components/userSelect/multi.vue"

export default {
  name: "Checkconfig",
  dicts: ['sys_yes_no'],
  components: {UserMultiSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSlCheckUser: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 复核配置表格数据
      checkconfigList: [],
      // 复核人表格数据
      slCheckUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userCount: null,
        containhrCount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询复核配置列表 */
    getList() {
      this.reset();
      const checkcfgId = 1;
      getCheckconfig(checkcfgId).then(response => {
        if(response.data!=undefined && response.data!=null){
          this.form = response.data;
          this.slCheckUserList = response.data.slCheckUserList;
        }

      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkcfgId: null,
        userCount: 1,
        containhrCount: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.slCheckUserList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkcfgId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加复核配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkcfgId = row.checkcfgId || this.ids
      getCheckconfig(checkcfgId).then(response => {
        this.form = response.data;
        this.slCheckUserList = response.data.slCheckUserList;
        this.open = true;
        this.title = "修改复核配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.slCheckUserList = this.slCheckUserList;
          if(this.slCheckUserList.length === 0){
            this.$modal.msgError("请至少选择一个复核人。");
            return;
          }
          if (this.form.checkcfgId != null) {
            updateCheckconfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckconfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkcfgIds = row.checkcfgId || this.ids;
      this.$modal.confirm('是否确认删除复核配置编号为"' + checkcfgIds + '"的数据项？').then(function() {
        return delCheckconfig(checkcfgIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 复核人序号 */
    rowSlCheckUserIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复核人添加按钮操作 */
    handleAddSlCheckUser() {
      this.$refs.userSelect.showFlag = true;
      // let obj = {};
      // obj.checkUser = "";
      // obj.checkNick = "";
      // obj.isHr = "";
      // this.slCheckUserList.push(obj);
    },
    /** 复核人删除按钮操作 */
    handleDeleteSlCheckUser() {
      if (this.checkedSlCheckUser.length == 0) {
        this.$modal.msgError("请先选择要删除的复核人数据");
      } else {
        const slCheckUserList = this.slCheckUserList;
        const checkedSlCheckUser = this.checkedSlCheckUser;
        this.slCheckUserList = slCheckUserList.filter(function(item) {
          return checkedSlCheckUser.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSlCheckUserSelectionChange(selection) {
      this.checkedSlCheckUser = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/checkconfig/export', {
        ...this.queryParams
      }, `checkconfig_${new Date().getTime()}.xlsx`)
    },
    userSelected(rows) {
      if (rows != null) {
        rows.forEach(user => {
          let obj = {};
          obj.checkUser = user.userName;
          obj.checkNick = user.nickName;
          if (isUniqueUser(this.slCheckUserList, obj)) {
            this.slCheckUserList.push(obj);
          }
        })
      }
    },
  }
};
</script>
